package com.quick.threadtest;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest {
    public static void main(String[] args) {
        /**
         * 核心线程数是5，最大线程池大小10、corePoolSize最大空闲线程存活时间100分钟
         * 阻塞队列长度10
         * 当循环小于等于20时无异常，
         * 当大于20时，抛出异常，多余出来的线程无法存入有界队列中，但是前20次线程执行不受影响
         */
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5,
                10,200l,
                TimeUnit.MINUTES,new ArrayBlockingQueue<Runnable>(10));
        for (int i = 0; i < 21; i++) {
            MyTask myTask = new MyTask(i);
            executor.execute(myTask);
            System.out.println("线程池中线程数目："+executor.getPoolSize()+"，队列中等待执行的任务数目："+
                    executor.getQueue().size()+"，已执行玩别的任务数目："+executor.getCompletedTaskCount());
        }
        executor.shutdown();
    }
}
class MyTask implements Runnable{
    private int taskNum;
    public MyTask(int taskNum){
        this.taskNum = taskNum;
    }
    @Override
    public void run() {
        System.out.println("task="+taskNum);
        try {
            Thread.currentThread().sleep(4000);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("执行完毕");
    }
}
